#!/usr/bin/python2.5
# Copyright (c) 2008 MalFind

__author__ = "noe.lutz@gmail.com (Noe Lutz)"

import unittest
from url_lookup import LookupUrlGenerator

class LookupUrlGeneratorUnittest(unittest.TestCase):
    """ Unittest for the class LookupUrlGenerator """
    def setUp(self):
        self.lg = LookupUrlGenerator()

    def testGetUrlCombinations(self):
        """ Test method for _GetUrlCombinations """
        # Two examples from the safe browsing API wiki page.
        testCases = [ [ 'http://a.b.c/1/2.html?param=1',
                        [ 'a.b.c/1/2.html?param=1',
                          'a.b.c/1/2.html',
                          'a.b.c/',
                          'a.b.c/1/',
                          'b.c/1/2.html?param=1',
                          'b.c/1/2.html',
                          'b.c/',
                          'b.c/1/' ]],
                      [ 'http://a.b.c.d.e.f.g/1.html',
                        [ 'a.b.c.d.e.f.g/1.html',
                          'a.b.c.d.e.f.g/',
                          'c.d.e.f.g/1.html',
                          'c.d.e.f.g/',
                          'd.e.f.g/1.html',
                          'd.e.f.g/',
                          'e.f.g/1.html',
                          'e.f.g/',
                          'f.g/1.html',
                          'f.g/' ]],
                      ]
        for test_value, expected in testCases:
            result = self.lg._GetUrlCombinations(test_value)
            self.assertEqual(len(result), len(expected),
                "Wrong number of returned urls.  Expected:%s, actual:%s" % \
                (expected, result))
            for res, exp in zip(sorted(result), sorted(expected)):
                self.assertEqual(res, exp,
                    "Expected:%s, actual:%s" % (expected, result))

    def testUrlCanonicalization(self):
        """ Test method for _UrlCanonicalization """
        testCases = [
            # Must have a path component
            [ 'http://www.google.com', 'http://www.google.com/' ],
            # Remove all leading and trailing dots
            [ 'http://...a.b.c.../', 'http://a.b.c/' ],
            # Replace consecutive dots with single dot
            [ 'http://a..b.c/', 'http://a.b.c/' ],
            # Lowercase the whole string
            [ 'http://A.B/', 'http://a.b/' ],
            # Resolve occurences of /../ and /./ in the path
            [ 'http://a.b/c/../d', 'http://a.b/d' ],
            [ 'http://a.b/c/d/../../e', 'http://a.b/e' ],
            [ 'http://a.b/c/../d/../e', 'http://a.b/e' ],
            [ 'http://a.b/c/./d/./e/./', 'http://a.b/c/d/e/' ],
            [ 'http://a.b/c//d//', 'http://a.b/c/d/' ],
            # Test percent-encoding
            [ 'http://a.b/c%2Ad', 'http://a.b/c*d' ],
            [ 'http://a.b/c%a6d', 'http://a.b/c%A6d' ],
            [ 'http://a.b/c%d',  'http://a.b/c%25d' ]
            ]
        for test_value, expected in testCases:
            result = self.lg._UrlCanonicalization(test_value)
            self.failUnlessEqual(expected, result,
                'Tested url:%s, Expected result:%s, Actual result: %s' % \
                (test_value, expected, result))

if __name__ == '__main__':
    unittest.main()
